package cn.bupt.sse309.ydl.activity;

import cn.bupt.sse309.ydl.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity implements OnClickListener {

  private Button achieveVerificationCodeBtn, verifyBtn, nextBtn,backBtn;
  private TextView resultTv, userProtocolEt, userProtocolTv,titleTv;
  private EditText phoneNumberEt, verificationCodeEt, passwordEt, confirmPasswordEt, inviteCodeEt;


  // private ImageView JIV_portrait;
  // 填写从短信SDK应用后台注册得到的APPKEY
  private static String APPKEY = " ";
  // 填写从短信SDK应用后台注册得到的APPSECRET
  private static String APPSECRET = " ";
  public String phoneNumberStr;

  // 头像图片路径
  private String portraitPath;

  private int recLen = 60;  //获取验证码倒计时

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    this.setContentView(R.layout.activity_register);
    this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.base_title_with_back);
    this.initView();

  }

  public void initView() {
    this.achieveVerificationCodeBtn = (Button) this.findViewById(R.id.btn_achieve);
    this.verifyBtn = (Button) this.findViewById(R.id.btn_verification);
    this.passwordEt = (EditText) this.findViewById(R.id.et_password);
    this.confirmPasswordEt = (EditText) this.findViewById(R.id.et_confirm_password);
    this.inviteCodeEt = (EditText) this.findViewById(R.id.et_invite_code);
    this.resultTv = (TextView) this.findViewById(R.id.tv_result);
    this.phoneNumberEt = (EditText) this.findViewById(R.id.et_phonenumber);
    this.verificationCodeEt = (EditText) this.findViewById(R.id.et_verification_code);
    this.nextBtn = (Button) this.findViewById(R.id.btn_next);

    this.userProtocolTv = (TextView) this.findViewById(R.id.tv_userprotocol);
    this.userProtocolTv.setClickable(true);
    this.userProtocolTv.setFocusable(true);
    this.userProtocolTv.setOnClickListener(this);

    this.achieveVerificationCodeBtn.setOnClickListener(this);
    this.verifyBtn.setOnClickListener(this);
    this.nextBtn.setOnClickListener(this);

    this.titleTv = (TextView) this.findViewById(R.id.baseTitleId);
    this.titleTv.setText(R.string.title_user_register);

    this.backBtn = (Button) this.findViewById(R.id.baseBack);
    this.backBtn.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        RegisterActivity.this.finish();
      }
    });



  }

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.btn_achieve:// 获取验证码

      RegisterActivity.this.phoneNumberStr = RegisterActivity.this.phoneNumberEt.getText()
          .toString();
      if (RegisterActivity.this.phoneNumberEt.length() <= 0) {
        Toast.makeText(this, "电话不能为空", 1).show();
        return;
      } else if (!RegisterActivity.this.phoneNumberStr.matches("^1(3|5|7|8|4)\\d{9}")) {

        Toast.makeText(this, "电话号码有误！！！", 1).show();
        return;
      } else {

        this.phoneNumberStr = this.phoneNumberEt.getText().toString();

        Message message = this.handler.obtainMessage(1);     // Message
        this.handler.sendMessageDelayed(message, 1000);
      }

      break;
    case R.id.btn_verification:// 校验验证码
      if (!TextUtils.isEmpty(this.verificationCodeEt.getText().toString())) {
        // SMSSDK.submitVerificationCode("86", this.phonenumber,
        // this.verificationCodeEt.getText().toString());
      } else {
        Toast.makeText(this, "验证码不能为空", 1).show();
      }
      break;
    case R.id.cb_userprotocol:
      break;
    case R.id.tv_userprotocol:
      Intent intent = new Intent(RegisterActivity.this, UserProtocolActivity.class);
      RegisterActivity.this.startActivity(intent);
      break;
    case R.id.btn_next:// 下一步
      Toast.makeText(this, "下一步", 1).show();
      Intent intent2 = new Intent(RegisterActivity.this, PersonalImfoActivity.class);
      RegisterActivity.this.startActivity(intent2);
      break;
    // case R.id.IV_portrait:
    // Intent intent = new Intent(SmsDemoActivity.this,
    // PicViewerActivity.class);
    // intent.putExtra("imagePath", SmsDemoActivity.this.portraitPath);
    // SmsDemoActivity.this.startActivity(intent);
    // break;
    default:
      break;
    }
  }

  Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
      // TODO Auto-generated method stub
//      super.handleMessage(msg);
      int event = msg.arg1;
      int result = msg.arg2;
      Object data = msg.obj;
      Log.e("event", "event=" + event);
      // if (result == SMSSDK.RESULT_COMPLETE) {
      // //短信注册成功后，返回MainActivity,然后提示新好友
      // if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//提交验证码成功
      // Toast.makeText(SmsDemoActivity.this.getApplicationContext(), "提交验证码成功",
      // Toast.LENGTH_SHORT).show();
      // SmsDemoActivity.this.resultTv.setText("提交验证码成功");
      // } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
      // Toast.makeText(SmsDemoActivity.this.getApplicationContext(), "验证码已经发送",
      // Toast.LENGTH_SHORT).show();
      // SmsDemoActivity.this.resultTv.setText("验证码已经发送");
      // }
      // else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){//返回支持发送验证码的国家列表
      // Toast.makeText(getApplicationContext(), "获取国家列表成功", Toast.LENGTH_SHORT).show();
      // countryTextView.setText(data.toString());
      //
      // }
      // } else {
      // ((Throwable) data).printStackTrace();
      // }



      switch (msg.what) {
      case 1:
          RegisterActivity.this.recLen--;
          RegisterActivity.this.achieveVerificationCodeBtn.setText("重新获取" + RegisterActivity.this.recLen+"秒");

          if(RegisterActivity.this.recLen > 0){
              Message message = RegisterActivity.this.handler.obtainMessage(1);
              RegisterActivity.this.handler.sendMessageDelayed(message, 1000);      // send message
              RegisterActivity.this.achieveVerificationCodeBtn.setClickable(false);
              RegisterActivity.this.achieveVerificationCodeBtn.setBackgroundColor(0x545454);

          }else{
            RegisterActivity.this.achieveVerificationCodeBtn.setClickable(true);

          }
      }

      super.handleMessage(msg);
  }



  };

  // //////////////////////////////////////////////////////////////////////////
  /*
   * 短信验证查看手机号码是否已注册
   *
   * 还没实现
   */

  public void doTaskCheckPhoneNumber() {
    // HashMap<String, String> urlParams = new HashMap<String, String>();
    // urlParams.put("phoneNumber", this.phonenumber);
    // urlParams.put("role", this.role);
    // try {
    // this.doTaskAsync(AppData.task.checkPhoneNumber, AppData.api.checkPhoneNumber, urlParams);
    // } catch (Exception e) {
    // e.printStackTrace();
    // }

  }

  @Override
  protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    // SMSSDK.unregisterAllEventHandler();

  }

  // /**
  // * 初始化数据,创建头像文件夹
  // * */
  // private void initData() {
  // // 初始化照片保存地址
  // if (Environment.getExternalStorageState().equals(
  // Environment.MEDIA_MOUNTED)) {
  // String thumPicture = AppData.dir.portrait;
  // File pictureParent = new File(thumPicture);
  // File pictureFile = new File(pictureParent, PICTURE_NAME);
  //
  // if (!pictureParent.exists()) {
  // pictureParent.mkdirs();
  // }
  // FileOutputStream outputStream = null;
  // if (!pictureFile.exists()) {
  // try {
  // pictureFile.createNewFile();
  //
  // this.portraitPath = pictureFile.getAbsolutePath();
  //
  // InputStream is = this.getResources().openRawResource(
  // R.drawable.bg_register_role_parent_normal);
  // Bitmap photo = BitmapFactory.decodeStream(is);
  // outputStream = new FileOutputStream(this.portraitPath);
  // photo.compress(Bitmap.CompressFormat.JPEG, 100,
  // outputStream);
  //
  // } catch (Exception e) {
  // e.printStackTrace();
  // } finally {
  // try {
  // outputStream.flush();
  // outputStream.close();
  // } catch (IOException e) {
  // e.printStackTrace();
  // }
  // }
  // } else {
  // this.portraitPath = pictureFile.getAbsolutePath();
  // }
  // this.JIV_portrait.setImageBitmap(this.compressImageFromFile(this.portraitPath));
  // } else {
  // Log.e("change user icon ==>>", "there is not sdcard!");
  // }
  // }

}






